package com.autumn.platform.auth.core.authc.impl;

import com.autumn.platform.core.context.response.impl.ResponseContext;
import com.autumn.platform.core.context.user.ISessionUser;

public class AuthenticationContext extends ResponseContext {

    /**
     * 
     */
    private static final long serialVersionUID = -4740920198371349667L;

    /**
     * 认证用户
     */
    private ISessionUser sessionUser;

    private boolean locked = false;

    public AuthenticationContext() {
        super();
    }

    public AuthenticationContext(String code, String message) {
        super(code, message);
    }

    public ISessionUser getSessionUser() {
        return sessionUser;
    }

    public <U extends ISessionUser> U getSessionUser(Class<U> cls) {
        if (null != sessionUser) {
            return cls.cast(sessionUser);
        }
        return null;
    }

    public void setSessionUser(ISessionUser sessionUser) {
        validate();
        this.sessionUser = sessionUser;
    }

    @Override
    public void setCode(String code) {
        validate();
        super.setCode(code);
    }

    /* package */void setLocked(boolean locked) {
        this.locked = locked;
    }

    protected void validate() {
        if (locked) {
            throw new IllegalStateException("The AuthenticationContext state is locked. If you need to modify the state, modify it in the IAuthenticationHandler implementation class");
        }
    }
}
